package com.jxs.android.sxyw;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.jxs.android.sxyw.data.BusinessInfo;
import com.jxs.android.sxyw.db.BusinessHolder;
import com.jxs.android.sxyw.weiget.TopBar;

/**
 * @Des 选择图片
 * @author Jiangxs
 * 
 */
public class SelectBusiness extends AbstractActivity implements OnItemClickListener {

	private ListView listView = null;
	private Context context;
	private List<BusinessInfo> businessInfos = new ArrayList<BusinessInfo>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.params_with_list);

		context = this;
		businessInfos = BusinessHolder.queryAll();
		listView = (ListView) findViewById(R.id.common_listview);
		listView.setAdapter(new AppAdpter(context));
		listView.setOnItemClickListener(this);
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		topBar.hindBtn();
		topBar.setTitle("业务");
		initTopBarReturn(topBar);
	}

	/**
	 * @Des 点击中间的图片的时候处理事件
	 */

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putString("business_name", businessInfos.get(position).getName());
		bundle.putString("business_id", businessInfos.get(position).getId());
		intent.putExtras(bundle);
		setResult(RESULT_OK, intent);
		this.finish();
	}

	private class AppAdpter extends BaseAdapter {

		private LayoutInflater inflater = null;

		public AppAdpter(Context context) {
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return businessInfos.size();
		}

		@Override
		public BusinessInfo getItem(int position) {
			return businessInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView textView = (TextView) inflater.inflate(R.layout.tv_item, null);
			if (position == 0) {
				textView.setBackgroundResource(R.drawable.roundlist_head);
			}
			if (position == businessInfos.size() - 1) {
				textView.setBackgroundResource(R.drawable.roundlist_tail);
			}
			textView.setText("        " + businessInfos.get(position).getName().trim());
			return textView;
		}

	}

}